package main

import (
	"bufio"
	"fmt"
	"os"
)

func ReadFileToChan(filePath string, ch chan string) {

	f, ok := os.Open(filePath)
	if ok != nil {
		fmt.Println(ok.Error(), "--", filePath)
		close(ch)
		return
	}
	go func() {
		err := ReadLine(f, ch)
		if err != nil {
			fmt.Println(err.Error())
			return
		}
	}()
}

func ReadLine(file *os.File, ch chan string) error {
	defer func(file *os.File) {
		err := file.Close()
		if err != nil {
		}
		close(ch)
	}(file)
	r := bufio.NewReader(file)
	for {
		line, err := readLine(r)
		if err != nil {
			break
		}
		ch <- line

	}
	return nil
}

func readLine(r *bufio.Reader) (string, error) {
	line, isPrefix, err := r.ReadLine()
	for isPrefix && err == nil {
		var bs []byte
		bs, isPrefix, err = r.ReadLine()
		line = append(line, bs...)
	}
	return string(line), err
}

type ProduceLine interface {
	ConvertLine(str string)
}
